#########################################################################################
####################################  FIGURE 2   ########################################
##################  Placebo Test of Women’s Influence and Authority  ####################
#########################################################################################

library(readstata13)
library(dplyr)
library(ggplot2)
library(randomizr)

rm(list=ls())

#setwd('C:/Users/ganth/Dropbox/StrengthInNumbersReplicationPackage/replicable/figure_2')

#########################################################################################

#load original data
indiv_data<-read.dta13("figure_2_dataset.dta")

# Subset to groups with 1 woman:
group_1f <- indiv_data %>% filter(fall_condition==1)

women_1f_mostvotes <- group_1f %>% filter(female=="F") %>% dplyr::summarize(mean = mean(as.numeric(mostvotes, na.rm = T)))
women_1f_mostspokesvotes <- group_1f %>% filter(female=="F") %>% dplyr::summarize(mean = mean(as.numeric(most_spokesvotes, na.rm = T)))

# Subset to all-male groups
group_0f <- indiv_data %>% filter(fall_condition==0)


#Empty dataframe for creating figure
placebo_0f_fig <- data.frame(matrix(ncol = 2, nrow =1))
colnames(placebo_0f_fig) <- c("mostvotes", "mostspokesvotes")

set.seed(9904)

for (i in 1:1500) {

  placebo_0f <- within(group_0f, {
    random_placebo <- block_ra(blocks = group_0f$group_id, prob_each = c(.8, .2))
  })


  #Computing percentage of random who had most votes and most spokesperson votes
  placebo_0f$placebo_mostvotes <- ifelse(placebo_0f$mostvotes==1 & placebo_0f$random_placebo==1,
                                    1, 0)
  placebo_0f$placebo_mostspokesvotes <- ifelse(placebo_0f$most_spokesvotes==1 & placebo_0f$random_placebo==1,
                                         1, 0)

  placebo_only_0f_mostvotes <- placebo_0f %>% filter(random_placebo==1) %>% dplyr::summarize(mean=mean(mostvotes, na.rm=T))
  placebo_only_0f_mostspokesvotes <- placebo_0f %>% filter(random_placebo==1) %>% dplyr::summarize(mean=mean(most_spokesvotes, na.rm=T))


  ##Making Figures

  temp.placebo_0f <- data.frame(matrix(ncol = 2, nrow =1))
  colnames(temp.placebo_0f) <- c("mostvotes", "mostspokesvotes")
  temp.placebo_0f$mostvotes <- placebo_only_0f_mostvotes
  temp.placebo_0f$mostspokesvotes <- placebo_only_0f_mostspokesvotes

  placebo_0f_fig <- rbind(placebo_0f_fig, temp.placebo_0f)

}


##Create figure
png(filename = "figure_2a.png", width = 1300, height = 800)

placebo_0f_fig <- placebo_0f_fig %>% filter(is.na(mostvotes)==F)
placebo_0f_fig$mostvotes <- as.numeric(placebo_0f_fig$mostvotes)
placebo_0f_fig$mostspokesvotes <- as.numeric(placebo_0f_fig$mostspokesvotes)

mostvotes_women <- women_1f_mostvotes$mean
mostspokesvotes_women <- women_1f_mostspokesvotes$mean

mostvotes_placebo_0f_med <- median(placebo_0f_fig$mostvotes, na.rm = T)
mostvotes_placebo_0f_mean <- mean(placebo_0f_fig$mostvotes, na.rm = T)
mostvotes_placebo_0f_sd <- sd(placebo_0f_fig$mostvotes, na.rm = T)
mostvotes_sd_away = (mostvotes_placebo_0f_mean-mostvotes_women)/mostvotes_placebo_0f_sd

mostspokesvotes_placebo_0f_med <- median(placebo_0f_fig$mostspokesvotes, na.rm = T)
mostspokesvotes_placebo_0f_mean <- mean(placebo_0f_fig$mostspokesvotes, na.rm = T)
mostspokesvotes_placebo_0f_sd <- sd(placebo_0f_fig$mostspokesvotes, na.rm = T)
mostspokesvotes_sd_away = (mostspokesvotes_placebo_0f_mean-mostspokesvotes_women)/mostspokesvotes_placebo_0f_sd


plot_placebo_0f<-ggplot(data=placebo_0f_fig)+
  geom_density(aes(mostvotes),color="gray",fill="gray",alpha=0.2)+
  labs(x="Proportion Chosen as Most Influential",y="Density",
       title="Most Influential Votes")+
  geom_vline(xintercept = mostvotes_women, color="blue", linetype = "dashed", alpha=0.8) +
  geom_vline(xintercept = mostvotes_placebo_0f_mean, color="darkgrey", linetype = "dashed") +
  expand_limits(x=c(0, .5)) +
  theme_bw()
plot_placebo_0f
dev.off()

quantile(placebo_0f_fig$mostvotes, probs = seq(0, 1, 0.01))


png(filename = "figure_2b.png", width = 1300, height = 800)
plot_placebo_0f_spokes<-ggplot(data=placebo_0f_fig)+
  geom_density(aes(mostspokesvotes),color="gray",fill="gray",alpha=0.2)+
  labs(x="Proportion Chosen as Spokesperson",y="Density",
       title="Most Spokesperson Votes")+
  geom_vline(xintercept = mostspokesvotes_women, color="blue", linetype = "dashed", alpha=0.8) +
  geom_vline(xintercept = mostspokesvotes_placebo_0f_mean, color="darkgrey", linetype = "dashed") +
  expand_limits(x=c(0, .5)) +
  theme_bw()
plot_placebo_0f_spokes
dev.off()




